<template>
	<view>
		<view>
			<messageBox ref="messageBox"></messageBox>
		</view>

		<view>
			<inputBox @sendMessage="handleMessage" @bottomHeight="handleBottomHeight"
				@keyboradHeight="handleKeyboradHeight" ref="inputBox" :callInfo="callInfo"></inputBox>
		</view>
	</view>
</template>

<script>
	import timeUtil from '@/util/timeUtil.js';
	import inputBox from '@/components/inputBox/inputBox.vue';
	import {
		mapActions,
		mapGetters
	} from 'vuex';
	import $store from '@/store/index.js';
	import userRequest from '@/api/user.js';
	export default {
		components: {
			inputBox: inputBox
		},
		data() {
			return {
				emoji: '',
				index: 0,
			}
		},
		onUnload() {
			console.log('退出聊天窗口')
			$store.state.chattingUserInfo = null
		},
		computed: {
			...mapGetters(['loginUserInfo', 'chattingUserInfo', 'personMessage', 'onlineArray']),
			callInfo() {
				return {
					remoteAvatar: this.chattingUserInfo.avatar,
					remoteName: this.chattingUserInfo.name,
					remoteId: this.chattingUserInfo.chattingUserId,
					loginId: this.loginUserInfo.id,
					loginAvatar: this.loginUserInfo.avatar,
					loginName: this.loginUserInfo.nickname,
					sessionId: this.chattingUserInfo.sessionId
				}
			}
		},
		onNavigationBarButtonTap(e) {
			if (e.index === 0) {
				uni.navigateTo({
					url: '/pages/info/friend-info?id=' + $store.state.chattingUserInfo.chattingUserId
				})
			}
		},
		watch: {
			onlineArray() {
				this.showOnline()
			}
		},
		onPageScroll(h) {
			//this.$refs.inputBox.showDrawer = 0
			if (h.scrollTop == 0) {
				this.$refs.messageBox.getHistoryMsg()
			}
		},
		onLoad() {
			if (this.chattingUserInfo == null) {
				uni.switchTab({
					url: '/pages/index/index'
				})
				return
			}
			///清空消息未读
			this.clearUnread()
			this.showOnline()
		},
		methods: {
			test() {
				console.log(this.callInfo)
			},
			//查看在线人数
			showOnline() {
				let tip = this.onlineArray.indexOf(this.chattingUserInfo.chattingUserId) === -1 ? '离线' : '在线'
				uni.setNavigationBarTitle({
					title: this.chattingUserInfo.name + '(' + tip + ')'
				})
			},
			async clearUnread() {
				let res = await userRequest.clearUnread({
					myId: this.loginUserInfo.id,
					friendId: this.chattingUserInfo.chattingUserId
				})
				//console.log(res)
				for (let i = 0; i < $store.state.sessionList.length; i++) {
					if ($store.state.sessionList[i].sessionId == this.chattingUserInfo.sessionId) {
						$store.state.sessionList[i].unread = 0
						break
					}
				}
				$store.dispatch($store.state.chattingUserInfo)
			},
			handleMessage(message) {
				//console.log('正在构造消息...',message)
				let m = {
					senderId: this.loginUserInfo.id,
					receiverId: this.chattingUserInfo.chattingUserId,
					sendTime: message.time,
					content: message.content,
					messageType: message.type,
					sessionId: this.chattingUserInfo.sessionId
				}
				let msg = {
					type: 'person-message',
					data: m
				}
				let _this = this
				uni.sendSocketMessage({
					data: JSON.stringify(msg),
					fail() {
						uni.showToast({
							title: '发送失败',
							icon: 'error'
						})
					},
					success() {
						// setTimeout(()=>{
						// 	_this.clearUnread();
						// },300)
					}
				})
			},
			handleBottomHeight(height) {
				this.$refs.messageBox.setBottomHeigth(height)
			},
			handleKeyboradHeight(height) {
				// console.log(height)
				this.$refs.messageBox.setKeyboardHeight(height)
			}
		},
		created() {
			//console.log(timeUtil.getFormatTime(new Date()))
		}
	}
</script>

<style lang="scss">
